package net.expectx.pay.controller;

import io.swagger.annotations.ApiOperation;
import net.expectx.common.single.base.BaseConstants;
import net.expectx.pay.common.AdminConstants;
import net.expectx.pay.common.AdminResult;
import net.expectx.pay.common.util.ExpectXUtil;
import net.expectx.pay.dao.bean.*;
import net.expectx.pay.rpc.api.MerchantApplicationService;
import net.expectx.pay.rpc.api.MerchantPaymentConfigService;
import net.expectx.pay.rpc.api.MerchantUserService;
import net.expectx.pay.rpc.api.SystemTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hxs
 */
@Controller
@RequestMapping(value = "/manager/merchantPaymentConfig")
public class MerchantPaymentConfigController {

    @Autowired
    private MerchantPaymentConfigService merchantPaymentConfigService;
    @Autowired
    private SystemTypeService systemTypeService;
    @Autowired
    private MerchantUserService merchantUserService;
    @Autowired
    private MerchantApplicationService merchantApplicationService;


    private static final Long PAY_WAY_CODE = 17L;

    @ApiOperation(value = "商户支付配置主页")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "merchantPaymentConfig/index";
    }

    @ApiOperation(value = "新增商户支付配置")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(ModelMap modelMap) {
        MerchantUserExample merchantUserExample = new MerchantUserExample();
        MerchantUserExample.Criteria userCriteria = merchantUserExample.createCriteria();
        userCriteria.andIsDeletedEqualTo(AdminConstants.NO);
        List<MerchantUser> merchantUsers = merchantUserService.selectByExample(merchantUserExample);
        modelMap.put("merchantUsers",merchantUsers);

        SystemTypeExample systemTypeExample = new SystemTypeExample();
        SystemTypeExample.Criteria criteria = systemTypeExample.createCriteria();
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        criteria.andTypeGroupIdEqualTo(PAY_WAY_CODE);
        List<SystemType> systemTypes = systemTypeService.selectByExample(systemTypeExample);
        modelMap.put("systemTypes", systemTypes);
        return "merchantPaymentConfig/modify";
    }

    @ApiOperation(value = "修改商户支付配置")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, ModelMap modelMap) {
        SystemTypeExample systemTypeExample = new SystemTypeExample();
        SystemTypeExample.Criteria criteria = systemTypeExample.createCriteria();
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        criteria.andTypeGroupIdEqualTo(PAY_WAY_CODE);
        List<SystemType> systemTypes = systemTypeService.selectByExample(systemTypeExample);
        modelMap.put("systemTypes", systemTypes);

        MerchantUserExample merchantUserExample = new MerchantUserExample();
        MerchantUserExample.Criteria userCriteria = merchantUserExample.createCriteria();
        userCriteria.andIsDeletedEqualTo(AdminConstants.NO);
        List<MerchantUser> merchantUsers = merchantUserService.selectByExample(merchantUserExample);
        modelMap.put("merchantUsers",merchantUsers);

        MerchantPaymentConfigExample merchantPaymentConfigExample = new MerchantPaymentConfigExample();
        MerchantPaymentConfigExample.Criteria criteria1 = merchantPaymentConfigExample.createCriteria();
        criteria1.andIsDeletedEqualTo(AdminConstants.NO);
        criteria1.andIdEqualTo(id);
        MerchantPaymentConfig merchantPaymentConfig = merchantPaymentConfigService.selectByExample(merchantPaymentConfigExample).get(0);
        modelMap.put("merchantPaymentConfig", merchantPaymentConfig);
        return "merchantPaymentConfig/modify";
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Object page(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            HttpServletRequest request) {
        MerchantPaymentConfigExample merchantPaymentConfigExample = new MerchantPaymentConfigExample();
        MerchantPaymentConfigExample.Criteria criteria = merchantPaymentConfigExample.createCriteria();
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        String merchantNo = request.getParameter("merchantNo");
        if (StringUtils.isNotBlank(merchantNo)) {
            criteria.andMerchantNoEqualTo(merchantNo);
        }
        merchantPaymentConfigExample.setPageIndex(page);
        merchantPaymentConfigExample.setPageSize(limit);
        merchantPaymentConfigExample.setOrderByClause("gmt_modified desc");

        List<MerchantPaymentConfig> merchantPaymentConfig = merchantPaymentConfigService.selectByExample(merchantPaymentConfigExample);
        long total = merchantPaymentConfigService.countByExample(merchantPaymentConfigExample);
        Map<String, Object> result = new HashMap<>(16);
        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", merchantPaymentConfig);
        return result;
    }

    @ApiOperation(value = "新增商户支付配置")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(MerchantPaymentConfig merchantPaymentConfig) {
        MerchantPaymentConfigExample merchantPaymentConfigExample = new MerchantPaymentConfigExample();
        MerchantPaymentConfigExample.Criteria criteria = merchantPaymentConfigExample.createCriteria();
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        criteria.andMerchantNoEqualTo(merchantPaymentConfig.getMerchantNo());
        criteria.andPayWayEqualTo(merchantPaymentConfig.getPayWay());
        if(merchantPaymentConfigService.countByExample(merchantPaymentConfigExample)!=0){
            return  new AdminResult(false,200,"该应用"+merchantPaymentConfig.getPayWay()+"支付已存在",null);
        }
        merchantPaymentConfig.setGmtCreate(System.currentTimeMillis());
        merchantPaymentConfig.setGmtModified(System.currentTimeMillis());
        merchantPaymentConfig.setIsDeleted(AdminConstants.NO);
        return merchantPaymentConfigService.insertSelective(merchantPaymentConfig);
    }


    @ApiOperation(value = "修改商户支付配置")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") Long id, MerchantPaymentConfig merchantPaymentConfig) {
        MerchantPaymentConfigExample merchantPaymentConfigExample = new MerchantPaymentConfigExample();
        MerchantPaymentConfigExample.Criteria criteria = merchantPaymentConfigExample.createCriteria();
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        criteria.andIdNotEqualTo(id);
        criteria.andMerchantNoEqualTo(merchantPaymentConfig.getMerchantNo());
        criteria.andPayWayEqualTo(merchantPaymentConfig.getPayWay());
        if(merchantPaymentConfigService.countByExample(merchantPaymentConfigExample)!=0){
            return  new AdminResult(false,200,"该应用"+merchantPaymentConfig.getPayWay()+"支付已存在",null);
        }
        merchantPaymentConfig.setGmtModified(System.currentTimeMillis());
        return merchantPaymentConfigService.updateByPrimaryKeySelective(merchantPaymentConfig);
    }

    @ApiOperation(value = "删除商户支付配置")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        MerchantPaymentConfigExample merchantPaymentConfigExample = new MerchantPaymentConfigExample();
        merchantPaymentConfigExample.createCriteria().andIdIn(ExpectXUtil.toList(ids, ","));
        return merchantPaymentConfigService.physicalRemoveByExample(AdminConstants.YES, merchantPaymentConfigExample);
    }

    @ApiOperation(value = "查询应用")
    @RequestMapping(value = "/getApplication/{merchantNo}", method = RequestMethod.GET)
    @ResponseBody
    public List<MerchantApplication> getApplication(@PathVariable("merchantNo") String merchantNo) {
        MerchantApplicationExample merchantApplicationExample = new MerchantApplicationExample();
        MerchantApplicationExample.Criteria criteria = merchantApplicationExample.createCriteria();
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        criteria.andMerchantNoEqualTo(merchantNo);
        return merchantApplicationService.selectByExample(merchantApplicationExample);
    }
}
